'''
Created on 2011-12-11

@author: jiangxd
'''
from Tkinter import *
from ttk import *
import os.path
import codecs
import random
import time
from dict import baidu
import db
import tkMessageBox

class DictationView:
	def __init__(self, master):
		self.mode = 'word' # or explain
		self.next_word = None
		self.position = 0
	
	def init_widgets(self, frame):
		self.master = frame
		
		commentfrm = Frame(frame)
		commentfrm.pack(side=TOP)
		Label(commentfrm,text="Dictates all words you have looked up",state=DISABLED).pack()

		self.word_bar = Frame(frame)
		self.word_bar.pack(side=TOP)
		
		self.word_var = StringVar(self.word_bar)
		self.word = Label(self.word_bar, font=("Helvetica", 20), borderwidth=1, textvariable=self.word_var, name="dictation-word",width=32)
		self.word.pack(side=LEFT)
		
		self.random_var = IntVar(self.word_bar)
		self.random_chk = Checkbutton(self.word_bar,variable=self.random_var,text="Random Order?",command=self.on_random_checked)
#		self.random_chk.select()
		self.random_chk.pack(side=RIGHT)
		
		self.phonetic_var = StringVar(frame)
		self.phonetic = Label(frame,textvariable=self.phonetic_var,name="dictation-phonetic",foreground="#AAAA88",font=("Helvetica", 16))
		self.phonetic.pack(side=TOP)
		
		self.scroll_frame = Frame(frame,name='dictation-scroll')
		self.scroll_frame.pack(side=TOP)

		self.explain = Text(self.scroll_frame,name="dictation-explain",height=28,state=DISABLED)
		self.explain.pack(side=LEFT)
		
		self.scroll = Scrollbar(self.scroll_frame)
		self.scroll.pack(side=RIGHT,fill=Y)
		self.explain.config(yscrollcommand=self.scroll.set)
		self.scroll.config(command=self.explain.yview)
		
		self.button_frame = Frame(frame)
		self.button_frame.pack(side=TOP)

		self.clean = Button(self.button_frame,text="Clean words",command=self.on_prepared_clean)
		self.clean.pack(side=LEFT)
		
		self.remove = Button(self.button_frame,text="Remove this word",command=self.on_remove_word)
		self.remove.pack(side=LEFT)

		self.reset = Button(self.button_frame,text="Reset",command=self.on_reset)
		self.reset.pack(side=LEFT)
	
		self.prev = Button(self.button_frame,text="Last word",command=self.on_prev_word)
		self.prev.pack(side=LEFT)

		self.next = Button(self.button_frame,text="Next word",command=self.on_next_word)
		self.next.pack(side=RIGHT)
		self.next.focus_set()

		self.on_reset()
		
	def on_prepared_clean(self,*args):
		if tkMessageBox.askokcancel("Clean", "Do you really wish to clean all words you looked up?"):
			db.clean_words()
			self.on_reset()

	def on_prev_word(self,*args):
		self.position -= 2
		if self.position < 0:
			self.position = 0
			self.prev.config(state=DISABLED)
		else:
			self.mode = 'word'
			self.on_next_word(*args)
		self.next.config(state=NORMAL)

	def on_next_word(self,*args):
		if self.mode == 'word':
			self.next_word = db.get_dictated_word_at_reverse(self.position)
			if self.next_word is None:
				self.next.config(state=DISABLED)
				return
			self.word_var.set(self.next_word['word'])
			self.explain.config(state=NORMAL)
			self.explain.delete(1.0, END)
			self.explain.config(state=DISABLED)
			self.phonetic_var.set("")
			if self.random_var.get() == 1:
				self.position = int(random.random() * db.get_dictated_size())
			else:
				self.position += 1
			self.mode = "explain"
		else:
			self.phonetic_var.set(self.next_word['phonetic'])
			self.explain.config(state=NORMAL)
			self.explain.delete(1.0, END)
			self.explain.insert(1.0, self.next_word['explain'])
			self.explain.config(state=DISABLED)
			self.mode = "word"
		if self.random_var.get() == 0:
			self.prev.config(state=NORMAL)
	
	def on_remove_word(self,*args):
		word = self.word_var.get()
		if len(word) > 0:
			db.remove_dictated_word(self.word_var.get())
			self.position -= 1
			if self.position < 0:
				self.position = 0
				self.prev.config(state=DISABLED)

	def on_reset(self,*args):
		self.next.config(state=NORMAL)
		self.word_var.set("")
		self.phonetic_var.set("")
		self.explain.config(state=NORMAL)
		self.explain.delete(1.0, END)
		self.explain.config(state=DISABLED)
		if self.random_var.get() == 1:
			random.seed(time.time())
			self.position = int(random.random() * db.get_dictated_size())
			self.prev.config(state=DISABLED)
		else:
			self.position = 0
			self.prev.config(state=NORMAL)
#		if db.get_size() == 0:
#			self.next.config(state=DISABLED)
#			self.clean.config(state=DISABLED)
#		else:
#			self.next.config(state=NORMAL)
#			self.clean.config(state=NORMAL)
		self.mode = 'word'
		
	def on_random_checked(self,*args):
		self.on_reset()
